package com.praveen.reminder;

import java.util.Calendar;
import java.util.Comparator;
import java.util.GregorianCalendar;

public class Utility {
	public static String pad(int num) {
		if (num < 10) {
			return "0" + Integer.toString(num);
		}
		return Integer.toString(num);
	}
	
	public static ReminderData getReminderDataObject(int entryNo, String k, String v) {
		ReminderData data = new ReminderData();
		data.setmEntryNo(entryNo);
		int end = 0;
		data.setmYear(Integer.parseInt(k.substring(end, end + 4))); 
		end += 4;
		data.setmMonth(Integer.parseInt(k.substring(end, end + 2)));
		end += 2;
		data.setmDay(Integer.parseInt(k.substring(end, end + 2)));
		end += 2;
		data.setmHour(Integer.parseInt(k.substring(end, end + 2)));
		end += 2;
		data.setmMinute(Integer.parseInt(k.substring(end, end + 2)));
		data.setmReminderText(v);
		return data;
	}
	
	public static class PairComparator implements Comparator<Pair<Integer, String>> {
		public int compare(Pair<Integer, String> lhs, Pair<Integer, String> rhs) {
			String ls = lhs.getSecond();
			String rs = rhs.getSecond();
			int res = ls.compareTo(rs);
			if (res == 0)
				return lhs.getFirst() - rhs.getFirst();
			else
				return res;
		}	
	}
	
	public static int isPast(ReminderData data, ReminderApplication app) {
		final Calendar c = Calendar.getInstance();
		GregorianCalendar cur = new GregorianCalendar(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
		GregorianCalendar toc = new GregorianCalendar (data.getmYear(), data.getmMonth() - 1, data.getmDay());
		if(cur.compareTo(toc) == 0) {
			if((data.getmHour() < c.get(Calendar.HOUR_OF_DAY)) || 
					((data.getmHour() == c.get(Calendar.HOUR_OF_DAY) && data.getmMinute() < c.get(Calendar.MINUTE)))) {
				return ReminderApplication.TIME_ERROR;
			}
		}
		else if (cur.compareTo(toc) > 0) {
			return ReminderApplication.DATE_ERROR;
		}
		return ReminderApplication.NO_ERROR;
	}
}
